﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 道路运输从业人员无纸化考试系统
{
    public partial class UserCheckForm : Form
    {
        public UserCheckForm()
        {
            InitializeComponent();
            
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                string userName = txtUserName.Text;
                if (string.IsNullOrWhiteSpace(userName) || userName != "666")
                {
                    MessageBox.Show("输入的账号或密码不正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                string pwd = txtPwd.Text;
                if (string.IsNullOrWhiteSpace(pwd))
                {
                    MessageBox.Show("输入的账号或密码不正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                string strNetData = GetNetDateTime();
                if (string.IsNullOrEmpty(strNetData))
                {
                    MessageBox.Show("系统异常，请重试！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                DateTime netDate = DateTime.Parse(strNetData);
                DateTime lastDate = getLastDate(netDate);
                string checkPwd = netDate.ToString("dd") + lastDate.ToString("dd");
                if (pwd != checkPwd)
                {
                    MessageBox.Show("输入的账号或密码不正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                this.DialogResult = DialogResult.OK;
                this.Hide();


            }
            catch (Exception)
            {
                MessageBox.Show("系统异常，请联系管理员！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }


        }

        private DateTime getLastDate(DateTime nowDate)
        {
            DateTime resDate = nowDate.AddDays(-1);
            if (nowDate.Day == 1 && (resDate.Day == 29 || resDate.Day == 31))
            {
                resDate = nowDate.AddDays(-2);
            }
            return resDate;
        }

        public string GetNetDateTime()
        {//获取网络时间
            WebRequest request = null;
            WebResponse response = null;
            WebHeaderCollection headerCollection = null;
            string datetime = string.Empty;
            try
            {
                request = WebRequest.Create("https://www.baidu.com");
                request.Timeout = 3000;
                request.Credentials = CredentialCache.DefaultCredentials;
                response = request.GetResponse();
                headerCollection = response.Headers;
                foreach (var h in headerCollection.AllKeys)
                {
                    if (h == "Date")
                    {
                        datetime = headerCollection[h];
                    }
                }
                return datetime;
            }
            catch (Exception) { return datetime; }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                }
                if (response != null)
                {
                    response.Close();
                }
                if (headerCollection != null)
                {
                    headerCollection.Clear();
                }
            }
        }



        private void BtnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定要退出吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
                
            }
        }

        private void UserCheckForm_Load(object sender, EventArgs e)
        {
            txtUserName.Focus();
        }
    }
}
